package the.broccoli.setting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.wisteria.framework.web.BusinessException;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import the.broccoli.setting.dto.ParamsCondition;
import the.broccoli.setting.dto.ParamsDTO;
import the.broccoli.setting.entity.Params;
import the.broccoli.setting.mapper.ParamsMapper;
import the.broccoli.setting.service.IParamsService;

import java.util.List;

/**
 * <p>
 * 参数 服务实现类
 * </p>
 */
@Service
public class ParamsServiceImpl extends AbstractSuperService<ParamsMapper, Params> implements IParamsService {

    /**
     * 获取参数值
     *
     * @param paramsCode 参数编码
     * @return 参数值
     */
    @Override
    public String getValue(String paramsCode) {
        if (StringUtils.hasText(paramsCode)) {
            Params params = getByCode(paramsCode);
            if (params != null) {
                return params.getParamsValue();
            }
        }
        return null;
    }

    /**
     * 获取参数值
     * @param tenantId      租户ID
     * @param paramsCode    参数编码
     * @return
     */
    @Override
    public String getValue(Integer tenantId, String paramsCode) {
        List<Params> params = this.baseMapper.selectList(new LambdaQueryWrapper<Params>()
                .eq(Params::getTenantId, tenantId)
                .eq(Params::getParamsCode, paramsCode)
        );
        if (!CollectionUtils.isEmpty(params)) {
            return params.get(0).getParamsValue();
        }
        return null;
    }

    /**
     * 获取参数详情
     * @param paramsId  参数ID
     * @return
     */
    @Override
    public ParamsDTO detailById(String paramsId) {
        Params params = getById(paramsId);
        return new ParamsDTO(params);
    }

    /**
     * 保存参数信息
     * @param entity    参数信息
     * @return
     */
    @Transactional
    @Override
    public boolean save(Params entity) {
        checkParams(entity);
        return super.save(entity);
    }

    /**
     * 修改参数
     * @param entity    参数信息
     * @return
     */
    @Transactional
    @Override
    public boolean updateById(Params entity) {
        checkParams(entity);
        return super.updateById(entity);
    }

    /**
     * 校验参数信息
     * @param entity    参数信息
     */
    private void checkParams(Params entity) {
        if (StringUtils.hasText(entity.getParamsCode()) && getByCode(entity.getParamsCode()) != null) {
            throw new BusinessException("参数编码已经存在相同的");
        }
    }

    /**
     * 根据参数编码获取参数信息
     * @param code  参数编码
     * @return
     */
    private Params getByCode(String code) {
        ParamsCondition condition = new ParamsCondition();
        condition.setParamsCode(code);
        List<Params> list = listByCondition(condition);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }
}
